$(function(){
    var checkDate,age,address,page,sex,allPage,zhi,searchKey;

    //页面初始化      加载地区列表   地区从后台获取;
    function getAreaList()
    {
        $.ajax({
            type:'get',
            headers: {"Accept-CMS-Token":token},
            url:arealist,
            dataType:'json',
            success:function (result){
                if(result.resultCode == 200)
                {
                    var areaList = result.value.list;
                    var len = areaList.length;
                    var str='';
                    for( var i=0;i<len;i++ )
                    {
                        str+='<option value="'+areaList[i].id+'">'+areaList[i].fullName+'</option>';
                    };
                    $('.areaList').append(str);
                    console.log('地区列表请求成功!!!!')
                }
                else if(result.resultCode == 515)
                {
                    reLogin();
                }
            },
            error:function(){
                console.log('地区列表请求失败')
            }
        });
    };
    getAreaList();

    //报告管理列表
    function report( checkDate,age,address,page,searchKey,sex,size )
    {
        $.ajax({
            async:false,
            cache:false,
            type:'post',
            headers: {"Accept-CMS-Token":token},
            url:report_list,
            data:{
                checkDate:checkDate,
                age:age,
                area:address,//跟接口文档的不一样,以这个为准;
                page:page,
                searchKey:searchKey,
                sex:sex,
                size:size
            },
            dataType:'json',
            success:successFn,
            error:function(){
                console.log('报告管理列表请求失败')
            }
        });
        return allPage;
    };
    report('','','',1,'','',10);
    getPage(allPage);

    // ajax请求成功后执行该函数
    function successFn(result)
    {
        if( result.resultCode == 200 )
        {
            var reportList=result.value.list;
            var len=reportList.length;
            allPage = result.count;
            clearTable();

            for( var i=0;i<len;i++ )
            {
                $('.reportList').append('<tr></tr>');

                $('.reportList tr').eq(i+1).append('<td>'+reportList[i].id+'</td>');

                $('.reportList tr').eq(i+1).append('<td>'+reportList[i].name+'</td>');

                $('.reportList tr').eq(i+1).append('<td>'+reportList[i].phone+'</td>');

                $('.reportList tr').eq(i+1).append('<td>'+sureSex(reportList[i].sex)+'</td>');

                $('.reportList tr').eq(i+1).append('<td>'+reportList[i].age+'</td>');

                $('.reportList tr').eq(i+1).append('<td>'+reportList[i].address.substr(0,2)+'</td>');

                $('.reportList tr').eq(i+1).append('<td>'+getMoth(reportList[i].reportTime)+'</td>');

                $('.reportList tr').eq(i+1).append('<td>'+getMoth(reportList[i].createDate)+'</td>');

                $('.reportList tr').eq(i+1).append('<td><a href="'+reportList[i].reportUrl+'" target="_blank">查看</a></td>');
            };

        }
        else if(result.resultCode == 515)
        {
            reLogin();
        }
        else
        {
            clearTable();
            $('.reportList').append('<tr><td></td><td></td><td>您搜索的内容不存在</td><td></td><td></td><td></td><td></td><td></td><td></td></tr>');
            hintUser();
            return allPage = 1;
        };
        console.log('报告管理列表请求成功!!!!')
    };

    //清空表格当中的数据,并且追加表头
    function clearTable()
    {
        $('.reportList').html('');
        $('.reportList').append('<tr></tr>');
        $('.reportList tr').eq(0).append('<th>用户ID</th><th>姓名</th><th>手机号</th><th>性别</th><th>年龄</th><th>地区</th><th>筛查时间</th><th>注册时间</th><th>报告</th>');
    };

    //整个页面的搜索功能!!!已经做好
    $('.report_admin_sou').click(function(){
        checkDate = $('.shaichaTime').val();
        age = $('.report_admin_age').children('option:selected').val();
        address = $('.areaList').children('option:selected').html();
        if( address == '全部')
        {
            address = '';
        };
        searchKey = $('input[name = report_admin_cont]').val();
        page = 1;
        sex = $('.user_admin_sex').children('option:selected').val();
        $('.reportList tr td').html('');
        zhi = report( checkDate,age,address,page,searchKey,sex,10 );
        getPage(zhi);
        // $('input[name = report_admin_cont]').val('');
    });

    //当输入文本框中时,,,按下enter键,,,发起搜索,,,按下键盘中的shift+enter键时向后台发起搜索,,
    $(document).keydown(function(event){
        var ev = ev || event;

        if( ev.keyCode == 13 )
        {
            checkDate = $('.shaichaTime').val();
            age = $('.report_admin_age').children('option:selected').val();
            address = $('.areaList').children('option:selected').html();
            if( address == '全部')
            {
                address = '';
            };
            searchKey = $('input[name = report_admin_cont]').val();
            page = 1;
            sex = $('.user_admin_sex').children('option:selected').val();
            $('.reportList tr td').html('');
            zhi = report( checkDate,age,address,page,searchKey,sex,10 );
            getPage(zhi);
            // $('input[name = report_admin_cont]').val('');
        };

    });

    //将毫秒时间格式化为yyyy-MM-dd;
    function getMoth(str)
    {
        var oDate = new Date(str),
            oYear=oDate.getFullYear(),
            oMonth = oDate.getMonth()+1,
            oDay = oDate.getDate(),
            oTime = getzf(oYear) +'-'+ getzf(oMonth) +'-'+ getzf(oDay);//最后拼接时间
        return oTime;
    };

    //将日期补0操作
    function getzf(num)
    {
        if(parseInt(num) < 10){
            num = '0'+num;
        }
        return num;
    };

    //分页插件配置        以及翻页,页面跳转;      已经做好!!!
    function getPage(allPage)
    {
        $('.M-box2').pagination({
            pageCount:allPage,
            jump:true,
            coping:true,
            homePage:'首页',
            endPage:'末页',
            prevContent:'上页',
            nextContent:'下页',
            callback:function (api){//当点击页面当中的时候,会出发这个callback函数;;;;
                page = api.getCurrent()+"";//得到的是要跳转到的那一页的页码;
                checkDate = $('.shaichaTime').val();
                age = $('.report_admin_age').children('option:selected').val();
                address = $('.areaList').children('option:selected').html();
                if( address == '全部' )
                {
                    address = '';
                };
                sex = $('.user_admin_sex').children('option:selected').val();
                report( checkDate,age,address,page,searchKey,sex,10 )
                // report( age,address,page,searchKey,sex,10 );
            }
        });
    };

    // 每一个下拉菜单发生变化的时候,,,,向后台请求,,,对应的列表发生变化;
    function changeXiaLa(x)
    {
        $(x).change(function (){
            checkDate = $('.shaichaTime').val();
            age = $('.report_admin_age').children('option:selected').val();
            address = $('.areaList').children('option:selected').html();
            if( address == '全部')
            {
                address = '';
            };
            searchKey = $('input[name = report_admin_cont]').val();
            page = 1;
            sex = $('.user_admin_sex').children('option:selected').val();
            $('.reportList tr td').html('');
            zhi = report( checkDate,age,address,page,searchKey,sex,10 );
            getPage(zhi);
        });
    };
    changeXiaLa('.areaList');
    changeXiaLa('.user_admin_sex');
    changeXiaLa('.report_admin_age');
    changeXiaLa('.shaichaTime');

    // 当后台状态码是515时,让客户重新登录
    function reLogin()
    {
        alert('登录超时,请重新登录!');
        localStorage.clear();
        window.location.href = login_jump;
    };

    //为侧边栏的标签增加active
    function setSideActive()
    {
        for(var m=0;m<$('.sub_list').length;m++)
        {
            for(var n=0;n<$('.sub_list').eq(m).children('li').length;n++)
            {
                if( $('.sub_list').eq(m).children('li').eq(n).children('a').attr('href').indexOf('report_admin.html') >= 0 )
                {
                    $('.sub_list').eq(m).children('li').eq(n).addClass('active');
                    $('.sub_list').eq(m).children('li').eq(n).parent('ul').prev('h3').css({ 'color':'#fff' })
                    $('#accordion h3').eq(m).attr('onOff',0)
                };

            };

        };
    };
    setSideActive();

    //页面初始化时 为一级菜单设置展开和对应的icon
    function setFirstUnfold()
    {
        for( var i=0;i<$('.sub_list').length;i++ )
        {
            if($('#accordion h3').eq(i).attr('onOff') == 1)
            {
                $('#accordion h3').eq(i).css({ 'color':'#aaa' })
                $('.sub_list').eq(i).hide();
            }
            else
            {
                $('#accordion h3').eq(i).css({ 'color':'#fff' })
                $('.sub_list').eq(i).show();
            };
        };
    };
    setFirstUnfold();

    //根据后台返回的数据0和1来判断性别是男还是女
    function sureSex(sex)
    {
        if(sex != null || sex != '')
        {
            if( sex ==0 )
            {
                return '男';
            }
            else
            {
                return '女';
            };
        };
    };




});
